package cn.edu.sdjzu.xg.bysj.service;


import cn.edu.sdjzu.xg.bysj.dao.DepartmentDao;
import cn.edu.sdjzu.xg.bysj.dao.TeacherDao;
import cn.edu.sdjzu.xg.bysj.domain.Department;
import cn.edu.sdjzu.xg.bysj.domain.School;
import cn.edu.sdjzu.xg.bysj.domain.Teacher;
import test.BysjException;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

public final class DepartmentService {
    private static DepartmentDao departmentDao= DepartmentDao.getInstance();
    private static TeacherDao teacherDao= TeacherDao.getInstance();
    private static DepartmentService departmentService=new DepartmentService();
    private DepartmentService(){}

    public static DepartmentService getInstance(){
        return departmentService;
    }

    public Collection<Department> findAll(List conditions_json_str, Pagination pagination) throws SQLException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Collection<Department> departments = departmentDao.findAll(connection,conditions_json_str,pagination);
        JdbcHelper.close(connection);
        return departments;
    }

    public int countAll(School school) throws SQLException {

        return departmentDao.countAll(school);
    }

    public Department find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Department department = departmentDao.find(id,connection);
        JdbcHelper.close(connection);
        return department;
    }

    public boolean update(Department department) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean updated= departmentDao.update(department,connection);
        JdbcHelper.close(connection);
        return updated;
    }

    public boolean add(Department department) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean added = departmentDao.add(department,connection);
        JdbcHelper.close(connection);
        return added;
    }

    public boolean delete(Integer id) throws SQLException , BysjException {
        Connection connection = JdbcHelper.getConn();
        int count = TeacherService.getInstance().countAll(DepartmentService.getInstance().find(id));
        if(count ==0){
            boolean deleted = departmentDao.delete(id,connection);
            return deleted;
        }else{
            throw new BysjException(DepartmentService.getInstance().find(id).getDescription()+"仍有下级属 性,不能删除");
        }
    }

    public boolean delete(Department department) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        int count = TeacherService.getInstance().countAll(department);
        if(count ==0){
            boolean deleted =departmentDao.delete(department,connection);
            return deleted;
        }else{
            throw new BysjException(department.getDescription()+"仍有下级属性,不能删除");
        }
    }
}

